﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.FPCAutomaticPlateSetting
{
    [FlowAttributes(nameof(PlateZAxis), 5)]
    public class PlateZAxis : TaskBase
    {
        #region 轴变量
        private AxisMotion _料盘升降Z轴;
        private Axis _料盘升降Z轴Entity;
        #endregion

        #region 上升沿
        #endregion

        #region 定时器
        private Ton _tonDelay = new Ton();
        #endregion

        #region 点位
        #endregion

        #region 输入点
        private NormalOpen _进料拉带到位感应;
        private NormalOpen _料盘到位感应;
        #endregion

        #region 输出点
        #endregion

        #region 报警变量
        #endregion

        #region 私有变量
        private long _targetPosition = 0;
        #endregion

        public PlateZAxis(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _料盘升降Z轴 = axisp.GetAxisMotion("料盘升降Z轴");

            _料盘升降Z轴Entity = axisp.GetAxisEntity("料盘升降Z轴");

            _进料拉带到位感应 = iop.GetNormalOpen("进料拉带到位感应");
            _料盘到位感应 = iop.GetNormalOpen("料盘到位感应");
            _temporary.Status_Z轴 = new StationStatusModel();
        }

        public override void EmergencyStop()
        {

        }

        public override void Handle()
        {
            if (!_temporary.SystemStatus.Auto && _料盘升降Z轴.StopMotion && _temporary.SystemStatus.AlarmStop)
            {
                _料盘升降Z轴.Continuous(false, false);
            }
            if (SignalOn(0) || (_temporary.Monocycle && _step == 0))
            {
                if (_料盘升降Z轴.HomeComplete)
                {
                    _料盘升降Z轴.Move(-20076, 30000);
                    if (_料盘升降Z轴.ArrivalLocation)
                    {
                        _temporary.Status_Z轴.Permit_Blowing = true;
                        _temporary.Status_Z轴.DischargeComplete = false;
                        NextStep();                    
                    }
                }
            }
            if (SignalOn(10) || (_temporary.Monocycle && _step == 10))
            {
                if ((_temporary.Status_Z轴.DischargeComplete && _temporary.Status_料盘固定.Permit_Blowing) || _temporary.Monocycle)
                {
                    _料盘升降Z轴.Continuous(true, false);
                    NextStep();
                }
            }
            if (SignalOn(20) || (_temporary.Monocycle && _step == 20))
            {
                if (_料盘到位感应.Execute())
                {
                    _料盘升降Z轴.Continuous(false, false);
                    NextStep(5);
                }
            }
            if (SignalOn(25) || (_temporary.Monocycle && _step == 25))
            {
                if (_temporary.Monocycle)
                {
                    _temporary.Monocycle = false;
                    _step = 0;
                }
                else
                {
                    if (_料盘升降Z轴Entity.UnitConversion.ActualPosition < 468D)
                    {
                        _targetPosition = DistanceToPulse(5.139D + _料盘升降Z轴Entity.UnitConversion.ActualPosition, _料盘升降Z轴Entity);
                        NextStep(5);
                    }
                    else
                    {
                        _temporary.Status_Z轴.Permit_TakeMaterials = true;
                        NextStep(25);
                    }
                }
            }
            //if (SignalOn(25))
            //{
            //    _tonDelay.Time(300, true);
            //    if (_tonDelay.Out)
            //    {
            //        _tonDelay.Time(300, false);
            //        _temporary.Status_Z轴.Permit_TakeMaterials = true;
            //        NextStep(25);
            //    }
            //}
            //if (SignalOn(30))
            //{
            //    if (!_料盘升降Z轴.StopMotion)
            //    {
            //        _targetPosition = _料盘升降Z轴Entity.Status.CurrentPostion - 5000;
            //        NextStep(20);
            //    }
            //}
            if (SignalOn(30))
            {
                _料盘升降Z轴.Move(_targetPosition, 30000);
                if (_料盘升降Z轴.ArrivalLocation)
                {
                    _temporary.Status_Z轴.Permit_TakeMaterials = true;
                    NextStep(20);
                }
            }
            if (SignalOn(50))
            {
                if (!_料盘升降Z轴.StopMotion)
                {
                    if (_temporary.Status_Z轴.WithdrawalComplete)
                    {
                        _temporary.Status_Z轴.Permit_TakeMaterials = false;
                        _targetPosition = _料盘升降Z轴Entity.Status.CurrentPostion - DistanceToPulse(15, _料盘升降Z轴Entity);
                        if (_料盘升降Z轴Entity.UnitConversion.ActualPosition >= 468D)
                        {
                            NextStep(30);
                        }
                        else
                        {
                            NextStep();
                        }
                    }
                }
            }
            if (SignalOn(60))
            {
                _料盘升降Z轴.Move(_targetPosition, 30000);
                if (_料盘升降Z轴.ArrivalLocation)
                {
                    _temporary.Status_Z轴.WithdrawalComplete = false;
                    NextStep();
                }
            }
            if (SignalOn(70))
            {
                if (!_temporary.Status_Z轴.Permit_TakeMaterials)
                {
                    _step = 10;
                }
            }
            if (SignalOn(80))
            {
                _料盘升降Z轴.Move(_targetPosition, 30000);
                if (_料盘升降Z轴.ArrivalLocation)
                {
                    _temporary.Status_Z轴.WithdrawalComplete = false;
                    _step = 0;
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {
                //if (_temporary.AxisHome && (_temporary.AxisNameHome == "料盘升降Z轴") && _进料拉带到位感应.Execute())
                //{
                //    _dialogHost.DialogShow("进料拉带到位感应有信号,料盘升降Z轴禁止回原！！！", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
                //    _log.Error("进料拉带到位感应有信号,料盘升降Z轴禁止回原！！！");
                //    _temporary.SystemStatus.AlarmStop = true;
                //}
            }
        }

        public override void Resetting()
        {
            if (!_料盘到位感应.Execute() && _step == 20)
            {
                _step = 10;
            }
        }

        public override void SafeStop()
        {

        }
    }
}
